package com.example.springstudyvue.result;

import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.experimental.Accessors;

/**
 * @Author: linKe
 * @Date: 2021/11/25 8:14
 * @FileName: Result
 * @version: 1.0
 * @Description: 全局消息响应类
 */
@Data
@NoArgsConstructor
@AllArgsConstructor
@Accessors(chain = true)
@ApiModel(value = "全局响应对象",description = "用于返回前端所需的包装对象")
public class Result<T>{
    @ApiModelProperty(value = "响应状态码",notes = "成功时响应200，其它为错误")
    public String code;
    @ApiModelProperty(value = "响应消息",notes = "成功响应ok，其它为错误")
    public String msg;
    @ApiModelProperty(value = "响应实体对象",notes = "可为空，单一对象，或者集合，数组等")
    public T data;


    /**
     * @param flag 操作成功与否的状态    新增，删除，更新操作时返回使用
     * @return  消息结果集
     */
    public static Result<Boolean> success(boolean flag) {
        Result<Boolean> result = new Result<>(null,null,flag);
        if (flag) {
            result.setCode("200");
            result.setMsg("成功");
        }else {
            result.setCode("400");
            result.setMsg("失败");
        }
        return result;
    }

    /**
     *
     * @param data  DTO对象
     * @param <T>   自定义服装DTO类型
     * @return  带数据的结果集，主要用于查询
     */
    public static <T> Result<T> data(T data) {
        Result<T> result = new Result<T>();
        if(data != null){
            result.setCode("200");
            result.setMsg("成功");
        }else {
            result.setCode("400");
            result.setMsg("当前数据为空");
        }
        result.setData(data);
        return result;
    }


}
